package com.ynns.springcloud.controller;

import com.ynns.springcloud.pojo.Dept;
import com.ynns.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

//提供resultful服务
@RestController
@RequestMapping("/dept")
public class DeptController {
    //获取具体信息得到具体的微服务
    @Autowired
    private DeptService deptService;

    @Autowired
    private DiscoveryClient client;

    @PostMapping("/add")
    public boolean addDept(Dept dept){
        System.out.println("------------>添加部门："+dept);
        return deptService.addDept(dept);
    }
    @GetMapping("/queryById/{id}")
    public Dept queryById(@PathVariable("id") Long id){
        System.out.println("------------>根据ID查询部门："+id);
        return deptService.queryById(id);
    }
    @GetMapping("/queryAll")
    public List<Dept> queryAll(){
        List<Dept> list=new ArrayList<Dept>();
        list=deptService.queryAll();
        for (Dept dept : list) {
            System.out.println("------------>查询全部部门："+dept);
        }
        return list;
    }

    //注册进来的微服务，获取一些消息
    @GetMapping("/discovery")
    public Object discovery(){
        //获取微服务列表清单
        List<String> services= client.getServices();
        System.out.println("service==>"+services);
        //得到一个具体的微服务信息，通过具体微服务id，applicationName
        List<ServiceInstance> instances=client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost()+"\t"+
                    instance.getPort()+"\t"+
                    instance.getUri()+"\t"+
                    instance.getServiceId()
            );
        }
        return this.client;
    }


}
